﻿Ext.util.Dispatcher = Ext.extend(Ext.util.Observable, {

    constructor: function (config) {
        this.addEvents(

            'before-dispatch',


            'dispatch'
        );

        Ext.util.Dispatcher.superclass.constructor.call(this, config);
    },

    // 跳转
    dispatch: function (options) { //Ext.redirect(token || this.defaultUrl || { controller: 'application', action: 'index' });
        var interaction = new Ext.Interaction(options), // 交互类
            controller = interaction.controller, // 控制器
            action = interaction.action, // 动作
            History = Ext.History;

        if (this.fireEvent('before-dispatch', interaction) !== false) {

            // 如果指定了锚点
            if (History && options.historyUrl) {
                History.suspendEvents(false); //暂停所有事件的触发
                History.add(options.historyUrl); // 改变hash（会触发事件）
                Ext.defer(History.resumeEvents, 100, History); // 恢复事件的触发
            }

            // 如果定义了控制器和动作
            if (controller && action) {
                controller[action].call(controller, interaction); // 运行控制器下的动作
                interaction.dispatched = true;
            }

            this.fireEvent('dispatch', interaction);
        }
    },

    // 重定向 Dispatches to a controller/action pair, adding a new url to the History stack
    redirect: function (options) {
        if (options instanceof Ext.data.Model) {


        } else if (typeof options == 'string') {

            //Recognizes(确认) a url string connected to the Router, 
            //return the controller/action pair plus any additional config associated with it
            var route = Ext.Router.recognize(options); // 辨认满足条件的url，转换成对象

            if (route) {
                return this.dispatch(route); // 跳转到制定页面
            }
        }
        return null;
    },


    createRedirect: function (url) {
        return function () {
            Ext.Dispatcher.redirect(url);
        };
    }
});


Ext.Dispatcher = new Ext.util.Dispatcher();


Ext.dispatch = function () {
    return Ext.Dispatcher.dispatch.apply(Ext.Dispatcher, arguments);
};


Ext.redirect = function () {
    return Ext.Dispatcher.redirect.apply(Ext.Dispatcher, arguments);
};

Ext.createRedirect = Ext.Dispatcher.createRedirect;